package org.balthie.demo.jvm.runtime.falsesharing;

//两个线程访问不同对象， count1，count2不在一个缓存行 
//total time: 563
//total time: 569
public class FalseSharingExample02
{
    public static void main(String[] args)
    {
        Counter counter1 = new Counter();
        Counter counter2 = new Counter();
        
        long iterations = 100_000_000;
        
        Thread thread1 = new Thread(() -> {
            long startTime = System.currentTimeMillis();
            for(long i = 0; i < iterations; i++)
            {
                counter1.count1++;
            }
            long endTime = System.currentTimeMillis();
            System.out.println("total time: " + (endTime - startTime));
        });
        Thread thread2 = new Thread(() -> {
            long startTime = System.currentTimeMillis();
            for(long i = 0; i < iterations; i++)
            {
                counter2.count2++;
            }
            long endTime = System.currentTimeMillis();
            System.out.println("total time: " + (endTime - startTime));
        });
        
        thread1.start();
        thread2.start();
    }
}